#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int MAXN=110;
int n,q,cnt,head[MAXN],f[MAXN][MAXN];
bool vis[MAXN];
struct edge
{
	int v,next,val;
}e[MAXN*2];
struct point
{
	int lc,rc;
}p[MAXN];
void addedge(int x,int y,int z)
{
	e[++cnt]=(edge){y,head[x],z};
	head[x]=cnt;
	return;
}
void dfs(int u)
{
	vis[u]=true;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].v;
		if(vis[v])continue;
		f[v][1]=e[i].val;
		p[u].lc?p[u].rc=v:p[u].lc=v;
		dfs(v);
	}
	return;
}
int treedp(int u,int k)
{
	if(~f[u][k])return f[u][k];
	int _max=0;
	for(int i=0;i<=k-1;++i)
	{
		int j=k-1-i,l=treedp(p[u].lc,i),r=treedp(p[u].rc,j);
		_max=max(_max,f[u][1]+l+r);
	}
	return f[u][k]=_max;
}
int main()
{
	n=read();q=read();
	for(int i=1;i<n;++i)
	{
		int x=read(),y=read(),z=read();
		addedge(x,y,z);
		addedge(y,x,z);
	}
	memset(f,-1,sizeof(f));
	dfs(1);
	for(int i=1;i<=n;++i)f[i][0]=0;
	f[1][1]=0;
	printf("%d\n",treedp(1,q+1));
	return 0;
}
